package com.hy.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import com.hy.constans.HttpStatus;
import com.hy.domain.User;
import com.hy.domain.dto.LoginFormDTO;
import com.hy.domain.dto.R;
import com.hy.domain.dto.RegisterFormDTO;
import com.hy.domain.dto.UserDTO;
import com.hy.exception.BusinessException;
import com.hy.service.UserService;
import com.hy.util.UserHolder;
import com.hy.util.WebsocketUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号/邮箱、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public R<UserDTO> login(@RequestBody LoginFormDTO loginForm){
        // TODO 实现登录功能
        return userService.login(loginForm);
    }

    /**
     * 修改密码
     * @param map 参数
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/modifyPwd")
    public R modifyPwd(@RequestBody Map map, HttpServletResponse response) {
        // 修改密码
        try {
            userService.updatePwd(map.get("oldPwd").toString(), map.get("newPwd").toString());
            response.setStatus(HttpStatus.UNAUTHORIZED);
            userService.logout();
            return R.ok().message("修改成功");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }

    @PostMapping("update")
    public R updateUser(@RequestBody UserDTO user, HttpServletRequest request) {
        userService.updateUserInfo(user, request);

        return R.ok().message("修改成功");
    }

    @GetMapping("/{userName}")
    public R<UserDTO> findById(@PathVariable Long userName) {
        final User user = userService.lambdaQuery().eq(User::getUserName, userName).one();
        final UserDTO userDTO = BeanUtil.toBean(user, UserDTO.class);

        return R.ok(userDTO);
    }

    /**
     * 查询用户列表
     * @param keyword
     * @return
     */
    @GetMapping("list/{keyword}")
    public R<List<UserDTO>> listByKeyword(@PathVariable String keyword) {
        List<User> user = userService.search(keyword);
        final List<UserDTO> dtoList = user.stream()
                .map(item -> BeanUtil.toBean(item, UserDTO.class))
                .collect(Collectors.toList());
        return R.ok(dtoList);
    }

    /**
     * 退出
     */
    @PostMapping("/logout")
    public R logout(){
        // TODO 实现登录功能
        return userService.logout();
    }

    /**
     * 获取登录信息
     */
    @GetMapping("/getInfo")
    public R<UserDTO> info(){
        UserDTO user = UserHolder.getUser();
        if (user == null) {
            throw new BusinessException (401, "用户未登录");
        }

        return R.ok(user);
    }

    /**
     * 注册功能
     * @param form
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterFormDTO form){
        // TODO 实现注册功能
        return userService.register(form);
    }
}
